package cn.itcast.servlet;

import cn.itcast.domain.City;
import cn.itcast.service.CityService;
import cn.itcast.service.impl.CityServiceImpl;
import com.fasterxml.jackson.databind.ObjectMapper;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;


@WebServlet("/city")
public class CityServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
      /*  System.out.println("ajax请求。。。。");*/
        CityService service = new CityServiceImpl();
        List<City> list = service.findAll();
        ObjectMapper obj = new ObjectMapper();
        String json = obj.writeValueAsString(list);
        System.out.println(json);
        resp.getWriter().write(json);
        //req.setAttribute("list",list);
        //req.getRequestDispatcher("/pages/add.jsp").forward(req,resp);
    }
}
